﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BaseDll;
using Communicate;
using log4net;
using SerialDict;
using MotionIoLib;
using System.Threading;
using System.ComponentModel;
using VisionBaseLib;
namespace OtherDevice
{
    [Description("HY视觉")]

    public class HYVisionControl:Vision_Base
    {
        public HYVisionControl(string strName) : base(strName)
        {
            controlFrom = new Frm_TcpIp();
        }
        private static readonly object Lock = new object();
        List<string> list_SnapOK = new List<string>();
        public override int linkEtherNetControl()
        {
            VisionBaseMgr.GetInstance().hyVision._GetVisionResultdelegate += ReciveMsg;
            VisionBaseMgr.GetInstance().hyVision._VisionSnapOK += 图像通知委托对象;
            return 0;
        }
        private void 图像通知委托对象(string 相机名称, string 流程名称)
        {
            if (!list_SnapOK.Contains(流程名称))
            {
                流程名称= 流程名称.Trim();
                list_SnapOK.Add(流程名称);
            }
        }
        public void ReciveMsg(List<string> 数据, string 相机名称, string 流程名称)
        {
            string strMsg = "";
            for (int i = 0; i < 数据.Count; i++)
            {
                if (i == 数据.Count - 1)
                    strMsg += 数据[i];
                else
                    strMsg += 数据[i] + ",";
            }
            InMessageQueue(strMsg);
        }
        #region 视觉算法调用
        public override int VisionControlUsing(string str_Meg, out string[] str_Result,Action actionMove=null, int nTimeout = 3000)
        {
            lock (Lock)
            {
                str_Result = null;
                list_SnapOK.Clear();
                clearMessageQueue();
                VisionBaseMgr.GetInstance().hyVision.Excute(str_Meg);
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Restart();
                while (true)
                {
                    Thread.Sleep(10);
                    if (list_SnapOK.Contains(str_Meg))
                    {
                        if (actionMove != null)
                            actionMove();
                        list_SnapOK.Clear();
                    }                    
                    string mes;
                    int ret = OutMessageQueue(out mes);

                    if (ret == 0)
                    {
                        //不需要注册函数，直接返回mes.X, mes.Y就可以
                        str_Result = mes.Split(',');
                        if (str_Result.Length == 0)
                            return -2;
                        else if (str_Result[str_Result.Length - 1] != "true")
                            return -1;
                        return 0;
                    }
                    else if (stopwatch.ElapsedMilliseconds > nTimeout)
                    {
                        return -4;
                    }

                }

            }
        }
        public override bool VisionExposureChange(string str_Meg, int nTimeout = 3000)
        {
            return true;
        }

        #endregion



    }
}